//本文地址：http://zhupeixin.top/article/2018/01/merge-json
var objC = {}.constructor;
function isJSON(obj) {
    return !!(obj && obj.constructor === objC);
}

var arrC = [].constructor;
function isArr(arr) {
    return !!(arr && arr.constructor === arrC);
}

function merge(json1,json2) {
    var result = null;
    if(isJSON(json2)){
        result = {};
        if(isJSON(json1)){
            for(var key in json1){
                if(json1.hasOwnProperty(key)){
                    result[key] = json1[key];
                }
            }
        }

        for(key in json2){
            if(json2.hasOwnProperty(key)){
                if(typeof result[key] === 'object' && typeof json2[key] === 'object'){
                    result[key] = merge(result[key],json2[key]);
                }else{
                    result[key] = json2[key];
                }
            }
        }
    }else if(isArr(json1) && isArr(json2)){
        result = json1;
        for(var i=0;i<json2.length;i++){
            if(typeof json2[i] === 'object'){
                result[i] = merge(result[i],json2[i]);
            }else{
                if(result.indexOf(json2[i]) === -1){
                    result.push(json2[i]);
                }
            }
        }
    }else{
        result = json2;
    }
    return result;
}

export default merge
